package com.eightbears.beboy.ui.merchant.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.entity.ShopsBean;
import com.eightbears.beboy.entity.status.Status_ShopDetail;
import com.eightbears.beboy.ui.user.activity.PaymentActivity;
import com.makeramen.roundedimageview.RoundedImageView;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.URL_SHOPS_DETAILS;
import static com.eightbears.beboy.ui.user.fragment.MessageFragment.PARAMS_QC_PAY_CODE;

/**
 * 商家支付
 */
public class ShopPaymentActivity extends BaseActivity {
    public static final String PARAMS_GET_MONEY_2_SHOP = "getMoney";

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_shop_payment_name)
    TextView tv_shop_payment_name;
    @BindView(R.id.iv_shop_payment_head)
    RoundedImageView iv_shop_payment_head;
    @BindView(R.id.et_shop_payment_cash)
    EditText et_shop_payment_cash;
    @BindView(R.id.ll_shop_payment_keyboard)
    LinearLayout ll_shop_payment_keyboard;
    private String mCash;
    private String mShopId;
    private ShopsBean mShopsBean;
    private Status_ShopDetail status_shopDetail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_payment);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        mShopId = (String) getIntent().getExtras().get(PARAMS_QC_PAY_CODE);
        tv_title.setText("付款");
        ll_shop_payment_keyboard.startAnimation(getTranslateAnimation(250 * 2, 0, 300));
    }

    @Override
    protected void initData() {
        getData();
    }

    @Override
    protected void initEvent() {

        et_shop_payment_cash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(et_shop_payment_cash.getWindowToken(), 0);
                et_shop_payment_cash.setInputType(0);
                ll_shop_payment_keyboard.startAnimation(getTranslateAnimation(250 * 2, 0, 300));
                et_shop_payment_cash.requestFocus();//获取焦点 光标出现
//                ll_shop_payment_keyboard.setVisibility(View.VISIBLE);
            }
        });
        //点击editveiw时，不弹出输入键盘
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    @OnClick(R.id.tv_shop_payment_1)
    void one() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (mCash.length() > 10) {
            return;
        }
        if (!TextUtils.isEmpty(mCash)) {
            et_shop_payment_cash.setText(mCash + "1");
        } else {
            et_shop_payment_cash.setText("1");
        }
        et_shop_payment_cash.setSelection(mCash.length() + 1);


    }

    @OnClick(R.id.tv_shop_payment_2)
    void two() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (mCash.length() > 10) {
            return;
        }
        if (!TextUtils.isEmpty(mCash)) {
            et_shop_payment_cash.setText(mCash + "2");
        } else {
            et_shop_payment_cash.setText("2");
        }
        et_shop_payment_cash.setSelection(mCash.length() + 1);
    }

    @OnClick(R.id.tv_shop_payment_3)
    void three() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (mCash.length() > 10) {
            return;
        }
        if (!TextUtils.isEmpty(mCash)) {
            et_shop_payment_cash.setText(mCash + "3");
        } else {
            et_shop_payment_cash.setText("3");
        }
        et_shop_payment_cash.setSelection(mCash.length() + 1);
    }

    @OnClick(R.id.tv_shop_payment_4)
    void four() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (mCash.length() > 10) {
            return;
        }
        if (!TextUtils.isEmpty(mCash)) {

            et_shop_payment_cash.setText(mCash + "4");
        } else {
            et_shop_payment_cash.setText("4");
        }
        et_shop_payment_cash.setSelection(mCash.length() + 1);
    }

    @OnClick(R.id.tv_shop_payment_5)
    void five() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (mCash.length() > 10) {
            return;
        }
        if (!TextUtils.isEmpty(mCash)) {
            et_shop_payment_cash.setText(mCash + "5");
        } else {
            et_shop_payment_cash.setText("5");
        }
        et_shop_payment_cash.setSelection(mCash.length() + 1);
    }

    @OnClick(R.id.tv_shop_payment_6)
    void six() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (mCash.length() > 10) {
            return;
        }
        if (!TextUtils.isEmpty(mCash)) {
            et_shop_payment_cash.setText(mCash + "6");
        } else {
            et_shop_payment_cash.setText("6");
        }
        et_shop_payment_cash.setSelection(mCash.length() + 1);
    }

    @OnClick(R.id.tv_shop_payment_7)
    void sevent() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (mCash.length() > 10) {
            return;
        }
        if (!TextUtils.isEmpty(mCash)) {
            et_shop_payment_cash.setText(mCash + "7");
        } else {
            et_shop_payment_cash.setText("7");
        }
        et_shop_payment_cash.setSelection(mCash.length() + 1);
    }

    @OnClick(R.id.tv_shop_payment_8)
    void eight() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (mCash.length() > 10) {
            return;
        }
        if (!TextUtils.isEmpty(mCash)) {
            et_shop_payment_cash.setText(mCash + "8");
        } else {
            et_shop_payment_cash.setText("8");
        }
        et_shop_payment_cash.setSelection(mCash.length() + 1);
    }

    @OnClick(R.id.tv_shop_payment_9)
    void nine() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (mCash.length() > 10) {
            return;
        }
        if (!TextUtils.isEmpty(mCash)) {
            et_shop_payment_cash.setText(mCash + "9");
        } else {
            et_shop_payment_cash.setText("9");
        }
        et_shop_payment_cash.setSelection(mCash.length() + 1);
    }

    @OnClick(R.id.tv_shop_payment_0)
    void zero() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (mCash.length() > 10) {
            return;
        }
        if (!TextUtils.isEmpty(mCash)) {
            et_shop_payment_cash.setText(mCash + "0");
        } else {
            et_shop_payment_cash.setText("0");
        }
        et_shop_payment_cash.setSelection(mCash.length() + 1);
    }

    @OnClick(R.id.iv_shop_payment_del)
    void del() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (!TextUtils.isEmpty(mCash)) {
            et_shop_payment_cash.setText("");
        }
    }

    @OnClick(R.id.iv_shop_payment_down)
    void down() {
        ll_shop_payment_keyboard.clearAnimation();
        ll_shop_payment_keyboard.setVisibility(View.GONE);
    }

    @OnClick(R.id.iv_shop_payment_dot)
    void dot() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (mCash.length() > 10) {
            return;
        }
        if (!TextUtils.isEmpty(mCash)) {
            et_shop_payment_cash.setText(mCash + ".");
        } else {
            et_shop_payment_cash.setText(".");
        }
        et_shop_payment_cash.setSelection(mCash.length() + 1);
    }

    @OnClick(R.id.tv_shop_payment_sure)
    void sure() {
        mCash = et_shop_payment_cash.getText().toString().trim();
        if (TextUtils.isEmpty(mCash)) {
            ShowToast.showShortToast("当前金额不能为空");
            return;
        }
        status_shopDetail.getData().setRepostMoney(mCash);
        startLocalActivity(
                ShopPaymentActivity.this, PaymentActivity.class, PARAMS_GET_MONEY_2_SHOP,  status_shopDetail);
        finish();
    }

    public void getData() {
        String url = URL_SHOPS_DETAILS + mShopId;
        OkHttpUtils.get().url(url)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .build().execute(new StringCallback() {
            @Override
            public void onResponse(String response) {
                try {
                    status_shopDetail = GsonUtil.fromJson(response, Status_ShopDetail.class);
                    Status_ShopDetail.DataBean data = status_shopDetail.getData();
                    tv_shop_payment_name.setText(data.getTitle());
                    Glide.with(mContext).load(data.getCover()).into(iv_shop_payment_head);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call request, Exception e) {
                KLog.e(e.getMessage());
            }


        });
    }

    /**
     * 设置动画
     */
    protected Animation getTranslateAnimation(int start, int end, int durationMillis) {
        Animation translateAnimation = new TranslateAnimation(0, 0, start, end);
        translateAnimation.setDuration(durationMillis);
        translateAnimation.setFillEnabled(true);
        translateAnimation.setFillAfter(true);
        return translateAnimation;
    }


}
